use std::{cell::LazyCell, collections::BTreeMap};

const FIB_COUNT: usize = 40;

const FIB_ODD: LazyCell<BTreeMap<u32, u32>> = LazyCell::new(|| {
    let mut fib = vec![1, 1];
    for _ in 2..FIB_COUNT {
        let l = fib.len();
        let curr = fib[l - 1] + fib[l - 2];
        fib.push(curr);
    }
    BTreeMap::from_iter(fib.into_iter().filter(|x| x % 2 == 1).scan(0, |acc, x| {
        *acc += x;
        Some((x, *acc))
    }))
});

pub fn odd_fibnacci_sum(threshold: u32) -> u32 {
    *FIB_ODD.range(..=threshold).last().unwrap().1
}
